#include "apue.h"
#include <fcntl.h>
int main(int argc, char *argv[]){
	int fd,val;
	if(argc != 2)
		err_sys("usage: ./3-4 <file description>");
	fd = atoi(argv[1]);
	val = fcntl(fd, F_GETFL, 0);
	if(val == -1)
		err_sys("fcntl error for fd %d\n",fd);
	switch(val & O_ACCMODE){
	case O_RDONLY:
		printf("read only");
		break;
	case O_WRONLY:
		printf("write only");
		break;
	case O_RDWR:
		printf("read, write");
		break;
	default:
		err_dump("unknown access mode");
	}
	if(val & O_APPEND)
		printf(", append");
	if(val & O_NONBLOCK)
		printf(", nonblock");
	#if defined(O_SYNC)
		if(val & O_SYNC)
			printf(", synchronous writes");
	#endif
	#if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC)
		if(val & O_FSYNC)
			printf(", fsync writes");
	#endif
	putchar('\n');
	exit(0);
}
